package com.flyme.module.base.provider.controller;

import cn.hutool.core.date.DateUtil;
import com.flyme.common.core.model.ResultBody;
import com.flyme.common.core.model.query.CriteriaQuery;
import com.flyme.common.core.model.query.PagerInfo;
import com.flyme.common.core.model.query.PqGrid;
import com.flyme.common.core.utils.ObjectUtils;
import com.flyme.core.base.supper.controller.BaseController;
import com.flyme.core.base.supper.entity.EntityMap;
import com.flyme.module.base.client.api.SysActivityrecordRemoteService;
import com.flyme.module.base.client.entity.*;
import com.flyme.module.base.provider.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * 用户活动申请记录管理
 * @author zyh
 * @date 2020-3-26
 */
@Api(tags = "用户活动申请记录")
@RestController
public class SysActivityrecordController extends BaseController<SysActivityrecordService, SysActivityrecord> implements SysActivityrecordRemoteService {

    @Autowired
    private SysPeriodrecordService periodrecordService;
    @Autowired
    private SysUsercertificateService usercertificateService;
    @Autowired
    private SysCertificateService certificateService;
    @Autowired
    private SysStudyconfigService studyconfigService;
    @Autowired
    private SysActivityrecordService activityrecordService;

    /**
     * 平台证书列表
     * @return
     */
    @Override
    public ResultBody list(PagerInfo pagerInfo, SysActivityrecord activityrecord) {
        CriteriaQuery cq = new CriteriaQuery<EntityMap>(pagerInfo);
        cq.select("a.id, a.activityName, a.activityType, a.activityTime, a.theme, a.activityAddress," +
                "a.sponsor, sc.certificateName, user.userName, a.period, a.createTime");
        cq.like("a.certificateName", activityrecord.getActivityName());
        cq.createAlias("user", "user");
        cq.createAlias("usercertificate", "usercertificate");
        cq.orderByDesc("a.createTime");
        PqGrid pqGrid = bizService.pageList(cq);
        return ResultBody.success(pqGrid);
    }

    /**
     * 编辑证书
     * @return
     */
    @ApiOperation(value = "编辑证书", notes = "编辑证书")
    @Override
    public ResultBody setStatus(SysActivityrecord activityrecord) {
        boolean result = false;
        //通过时，为用户添加学时，增加学时记录
        if(activityrecord.getActivityType().equals(1)){
            SysUsercertificate usercertificate = usercertificateService.getById(activityrecord.getUsercertificateId());
            SysStudyconfig studyconfig = studyconfigService.getByStudyConfig(usercertificate.getCertificateId());
            if(ObjectUtils.isEmpty(usercertificate.getSumperiod())){
                usercertificate.setSumperiod(0);
            }
            if(ObjectUtils.isEmpty(usercertificate.getUserperiod())){
                usercertificate.setUserperiod(0);
            }
            Integer sumperiod = usercertificate.getSumperiod() + usercertificate.getUserperiod();
            if(sumperiod >= studyconfig.getTagetPeriod()){
                return ResultBody.failed("用户该证书学时已满");
            }
            activityrecord.setPeriod(studyconfig.getOncePeriod());
            //继续教育开始时间
            EntityMap checkActivity = activityrecordService.checkActivity(usercertificate, studyconfig);
            //用户活动学时已超过
            if(checkActivity.get("checkType").equals(3)){
                return ResultBody.failed("用户活动学时已超过最大值");
            }
            //用户添加该次活动后就满活动学时
            if(checkActivity.get("checkType").equals(2)){
                return ResultBody.failed("用户添加活动后活动学时将超过最大值");
            }
            //用户添加该活动后活动学时不满
            if(checkActivity.get("checkType").equals(1)){
                activityrecord.setPeriod(studyconfig.getOncePeriod());
            }
            result = bizService.edit(activityrecord);
            //用户当前证书总学时
            Integer userNowPeriod = studyconfig.getOncePeriod() + usercertificate.getSumperiod() + usercertificate.getUserperiod();
            //满学时证书有效期向后延长一年
            if(userNowPeriod >= studyconfig.getTagetPeriod()){
                //用户当前总学时大于等于目标学时（证书改为满学时）
                usercertificate.setSumperiod(userNowPeriod);
                Date date = DateUtil.parse(usercertificate.getValidity());
                usercertificate.setIsFull(1);
                usercertificate.setValidity(DateUtil.formatDate(DateUtil.offsetMonth(date, 12)));
            }else{
                usercertificate.setSumperiod(studyconfig.getOncePeriod() + usercertificate.getSumperiod());
            }
            usercertificateService.saveOrUpdate(usercertificate);
        }else{
            activityrecord.setPeriod(0);
            result = bizService.edit(activityrecord);
        }
        return ResultBody.success(result);
    }

    /**
     * 获取活动详情
     * @return
     */
    @ApiOperation(value = "通过id查询")
    @Override
    public ResultBody get(@PathVariable Long id) {
        EntityMap map = new EntityMap();
        SysActivityrecord activityrecord = bizService.getById(id);
        map.put("activityrecord", activityrecord);
        map.put("activityImgs", activityrecord.getActivityImgs().split(","));
        return ResultBody.success().setData(map);
    }

}
